import Vue from 'vue'
import Router from 'vue-router'
import Home from '../pages/Home'
import About from '../pages/About'
import My from '../pages/My'
import Login from '../pages/Login'

const router = new Router({
    mode: "history",
    routes: [
        {
            path: "/home",
            component: Home
        },
        {
            path: "/about",
            component: About
        },
        {
            path: "/my",
            component: My
        },
        {
            path: "/login",
            component: Login
        }
    ]
})

// 路由拦截
router.beforeEach((to, from, next) => {
    // to     去往的地方
    // from   从哪里 
    // next   执行下一步    可以支持传参  next("/login")
    // console.log(to.path, from.path)
    if (to.path === '/my' && !sessionStorage.getItem('token')) {
        next("/login")
    } else {
        next()
    }
})

Vue.use(Router)

export default router